package dht.socket;

import java.util.Queue;

import dht.entity.Message;
import dht.entity.NodeInfo;

public class MessageDelayer implements Runnable {
	protected NodeInfo localInfo;
	protected Message msg;
	protected Queue<Message> msgQueue;

	public MessageDelayer(NodeInfo localInfo, Message msg,
			Queue<Message> msgQueue) {
		this.localInfo = localInfo;
		this.msg = msg;
		this.msgQueue = msgQueue;
	}

	@Override
	public void run() {
		try {
			Thread.sleep((long) (NodeInfo.cost(msg.getSenderNodeInfo(),
					localInfo, msg.getMessageSize()) * 1000));
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		synchronized (msgQueue) {
			msgQueue.add(msg);
			msgQueue.notify();
		}
	}

}
